package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.sqlite.entities.BroadcastHeaderEntity;
import com.onstar.cn.api.PublicService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Yonglang Hong on 2016/8/9.
 */
public class BroadcastDetailService extends BaseService {

    private Context context;

    public BroadcastDetailService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public Object jsonToBean(String json) {
        return null;
    }

    public Observable<BroadcastHeaderEntity> loadMsgDetail(final String msgId) {
        return Observable.defer(new Func0<Observable<BroadcastHeaderEntity>>() {
            @Override
            public Observable<BroadcastHeaderEntity> call() {
                try {
                    return Observable.just(doLoadMsgDetail(msgId));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public BroadcastHeaderEntity doLoadMsgDetail(String msgId) throws OnStarException {
        PublicService service = new PublicService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        int versionCode = new DeviceUtil(context).getAppVersionCode();
        service.BroadcastById(requestEntity, msgId,versionCode+"");

        requestEntity = HttpInjectUtils.injectHttp(getClass(), "doLoadMsgDetail", requestEntity,String.class);
        String response = NetConnectUtil.getResponse(getDatas(requestEntity));
        BroadcastHeaderEntity entity = GsonHelper.fromJson( response,BroadcastHeaderEntity.class);
        return entity;
    }
}
